//
//  YCTrainCarPlanViewController.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/27.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  练车计划

#import "YCTrainCarPlanViewController.h"
#import "YCTrainPlanCell.h"
#import "YCTrainPlanModel.h"
#import "YCTrainCarDetailViewController.h"

@interface YCTrainCarPlanViewController()<UITableViewDelegate,UITableViewDataSource>
/** tableView */
@property (nonatomic, strong) UITableView *tableView;

/** 数据  */
@property (nonatomic, strong) NSArray *itemsArray;

@end

@implementation YCTrainCarPlanViewController

//- (NSMutableArray *)itemsArray
//{
//    if (!_itemsArray) {
//        _itemsArray = [NSMutableArray array];
//        
//        YCTrainPlanModel *model = [[YCTrainPlanModel alloc] init];
//        model.finished = YES;
//        model.todayTrain = NO;
//        model.day = 15;
//        model.month = 4;
//        model.trainContent = @"静地训练 静机";
//        model.trainTimeStr = @"上午 09:00";
//        
//        YCTrainPlanModel *model1 = [[YCTrainPlanModel alloc] init];
//        model1.finished = NO;
//        model1.todayTrain = YES;
//        model1.day = 19;
//        model1.month = 4;
//        model1.trainContent = @"场地训练 半坡起步";
//        model1.trainTimeStr = @"上午 09:00";
//        
//        YCTrainPlanModel *model2 = [[YCTrainPlanModel alloc] init];
//        model2.finished = NO;
//        model2.todayTrain = NO;
//        model2.day = 24;
//        model2.month = 4;
//        model2.trainContent = @"场地训练 侧位停车";
//        model2.trainTimeStr = @"上午 09:00";
//        
//        YCTrainPlanModel *model3 = [[YCTrainPlanModel alloc] init];
//        model3.finished = NO;
//        model3.todayTrain = NO;
//        model3.day = 28;
//        model3.month = 4;
//        model3.trainContent = @"场地地训练 曲线行驶";
//        model3.trainTimeStr = @"下午 14:00";
//        
//        YCTrainPlanModel *model4 = [[YCTrainPlanModel alloc] init];
//        model4.finished = NO;
//        model4.todayTrain = NO;
//        model4.day = 30;
//        model4.month = 4;
//        model4.trainContent = @"场地地训练 直角转弯";
//        model4.trainTimeStr = @"上午 09:00";
//        
//        YCTrainPlanModel *model5 = [[YCTrainPlanModel alloc] init];
//        model5.finished = NO;
//        model5.todayTrain = NO;
//        model5.day = 5;
//        model5.month = 5;
//        model5.trainContent = @"场地地训练 倒车入库";
//        model5.trainTimeStr = @"下午 14:00";
//        
//        YCTrainPlanModel *model6 = [[YCTrainPlanModel alloc] init];
//        model6.finished = NO;
//        model6.todayTrain = NO;
//        model6.day = 9;
//        model6.month = 5;
//        model6.trainContent = @"场地地训练 路面行驶";
//        model6.trainTimeStr = @"晚上 18:00";
//        
//        [_itemsArray addObjectsFromArray:@[model,model1,model2,model3,model4,model5,model6]];
//        
//    }
//    return _itemsArray;
//}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.yc_width, self.view.yc_height - 64) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor yc_colorWithUInt:0xf2f2f2];
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([YCTrainPlanCell class]) bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:@"trainPlanCell"];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"练车计划";
    [self yc_addLeftBarButtonItemWithTitle:@"关闭" target:self action:@selector(back)];
    [self.view addSubview:self.tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(5, 0, 0, 0);
    self.tableView.rowHeight = 100;
    YCWeakSelf;
    self.tableView.mj_header = [YCRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 数据处理
- (void)loadData
{
    NSDictionary *params = @{
                             @"uid":@"1",
                             @"token":accessToken_Test,
                             };
    [[YCNetworManager shareManager] postWithUrl:YCAPITrainPlanList params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {
        self.itemsArray = [YCTrainPlanModel yc_objectArrayWithKeyValuesArray:response.data];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"%@",error.localizedDescription);
        [self.tableView.mj_header endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.itemsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCTrainPlanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"trainPlanCell"];
    cell.contentView.backgroundColor = [UIColor yc_colorWithUInt:0xf2f2f2];
    cell.itemModel = self.itemsArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCTrainCarDetailViewController *detailVc = [[YCTrainCarDetailViewController alloc] initWithModel:self.itemsArray[indexPath.row]];
    [self.navigationController pushViewController:detailVc animated:YES];
}


#pragma mark - 点击事件
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
